﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Beauty.Model;
using Beauty.BLL;
namespace WebUI.Models
{
    public class CommonMethod
    {
        #region 收藏
        //1、获取用户所有收藏 2、判断指定套图是否收藏  3、添加一个收藏 4、删除一个收藏
        public static List<collection> GetCollectionByUid(int uid)
        {
            return bllCollect.GetModelList("Uid=" + uid);
        }
        public static bool IsCollect(int uid, int albumId)
        {
            List<collection> coll = bllCollect.GetModelList(string.Format("Uid={0} AND AlbumId={1}", uid, albumId));
            if (coll.Count > 0)
            {
                return true;
            }
            return false;
        }
        public static bool AddCollection(int uid, int albumId)
        {
            return bllCollect.Add(new collection() { AlbumId = albumId, Uid = uid });
        }
        public static bool AddCollection(int uid, int albumId, string errMsg)
        {
            if (bllCollect.GetModelList("Uid=" + uid + "AND AlbumId=" + albumId).Count > 0)
            {
                errMsg = "该套图已收藏";
                return false;
            }
            return bllCollect.Add(new collection() { AlbumId = albumId, Uid = uid });
        }
        public static bool RemoveCollection(int uid, int albumId)
        {
            List<collection> coll = bllCollect.GetModelList(string.Format("Uid={0} AND AlbumId={1}", uid, albumId));
            if (coll.Count > 0)
            {
                return bllCollect.Delete(coll[0].Id);
            }
            return false;

        }

        static collectionBLL bllCollect = new collectionBLL();
        #endregion

        #region 是否登录
        public static bool CheckLogin(out user user)
        {
            user = HttpContext.Current.Session[DataConfig.UserSessionName] as user;
            if (user == null)
            {
                return false;
            }
            return true;
        }
        public static bool Logout()
        {
            if (HttpContext.Current.Session[DataConfig.UserSessionName] != null)
            {
                HttpContext.Current.Response.Cookies[DataConfig.AutoLoginCookie].Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Session.Remove(DataConfig.UserSessionName);
                return true;
            }
            else
            {
                return false;
            }

        }
        public static user UserInfoSession
        {
            get
            {
                user user;
                if (CheckLogin(out user))
                {
                    return user;
                }
                return null;
            }
        }
        #endregion
    }
}